package toolbox;

public class ByteConverter {
	public static byte[] longToBytes(long n){
		byte[] b = new byte[8];
		b[7] = (byte)(n&0xff);
		b[6] = (byte)(n>>8&0xff);
		b[5] = (byte)(n>>16&0xff);
		b[4] = (byte)(n>>24&0xff);
		b[3] = (byte)(n>>32&0xff);
		b[2] = (byte)(n>>40&0xff);
		b[1] = (byte)(n>>48&0xff);
		b[0] = (byte)(n>>56&0xff);
		return b;
	}
	public static long bytesToLong(byte[] b){
		return b[7]&0xff|(b[6]&0xff)<<8|(b[5]&0xff)<<16|(b[4]&0xff)<<24
				|(b[3]&0xff)<<32|(b[2]&0xff)<<40|(b[1]&0xff)<<48|(b[0]&0xff)<<56;
	}
	public static byte[] intToBytes(int n){
		byte[] b = new byte[4];
		b[3] = (byte)(n&0xff);
		b[2] = (byte)(n>>8&0xff);
		b[1] = (byte)(n>>16&0xff);
		b[0] = (byte)(n>>24&0xff);
		return b;
	}
	public static int bytesToInt(byte[] b){
		return b[3]&0xff|(b[2]&0xff)<<8|(b[1]&0xff)<<16|(b[0]&0xff)<<24;
	}
	public static byte[] intToBytes(int n,int i){
		byte[] b = new byte[4];
		b[0] = (byte)(n&0xff);
		b[1] = (byte)(n>>8&0xff);
		b[2] = (byte)(n>>16&0xff);
		b[3] = (byte)(n>>24&0xff);
		return b;
	}
	public static int bytesToInt(byte[] b, int i){
		return b[0]&0xff|(b[1]&0xff)<<8|(b[2]&0xff)<<16|(b[3]&0xff)<<24;
	}
}
